package middleware

import (
	"github.com/gin-gonic/gin"
	"rock/customer/public"
)

func Panic(c *gin.Context) {
	// 监控服务抛出异常
	defer func() {
		r := c.Errors.String()
		if r != "" {
			body, err := c.GetRawData()
			if err != nil {
				public.ContextError(c, "Error.GetRawData error", map[string]interface{}{
					"router":      c.Request.URL.Path,
					"ReadAll.err": err,
					"parameters":  string(body),
					"error":       r,
				})
			} else {
				public.ContextError(c, "Error error", map[string]interface{}{
					"router":     c.Request.URL.Path,
					"parameters": string(body),
					"error":      r,
				})
			}
			// 打印错误堆栈信息
			// fmt.Println(string(debug.Stack()))
			c.Abort()
		}
	}()
	c.Next()
}